package com.lei.service.impl;

import com.lei.dao.BookDao;
import com.lei.dao.impl.BookDaoImpl;
import com.lei.entity.Book;
import com.lei.service.BookService;
import com.lei.util.PageBean;

import java.util.List;

/**
 * @author Lei Chu
 * @Date 2020/10/21 18:32
 */
public class BookServiceImpl implements BookService {

    private final BookDao bookDao = new BookDaoImpl();

    @Override
    public List<Book> getBookIdAndNameByTypeId(int typeid) {
        return bookDao.getBookIdAndNameByTypeId(typeid);
    }

    @Override
    public Book getBookCountAndPriceById(String bookid) {
        return bookDao.getBookCountAndPriceById(bookid);
    }

    @Override
    public PageBean<Book> getAllBook(Book book) {
        PageBean<Book> page = new PageBean<>();
        long count = bookDao.getCount(book);
        if (count > 0) {
            page.setCount(count);
            page.setData(bookDao.getAllBook(book));
        }

        return page;
    }

    @Override
    public int updateStatus(Book book) {
        return bookDao.updateStatus(book);
    }

    @Override
    public int addBook(Book book) {
        // 检测这本书是否重复  条件----（书名、作者、出版社）
        if (bookDao.checkBook(book)) {
            if (book.getBookid() == null) {
                return bookDao.addBook(book);
            }
            return bookDao.updateBook(book);
        }
        // 名字不可用、返回 0
        return 0;
    }
}
